/**
 * Created by DELL on 2018/9/6.
 */

(function (ns) {

  var Keyboard = ns.Keyboard = Hilo.Class.create({
    Extends: Hilo.Container,
    constructor: function (properties) {
      Keyboard.superclass.constructor.call(this, properties);

      this.width = properties.stageWidth;
      this.height = 0;
      this.x = 0;
      this.y = properties.stageHeight / 4 * 2;
      this.stageWidth = properties.stageWidth;
      this.stageHeight = properties.stageHeight;
      this.moveTween = new Hilo.Tween(this, null, {
        onComplete: this.moveEnd.bind(this)
      });
      // this.init();
    },
    startKey: null,
    total_height: 0, // 按键wrapper总高度

    init: function () {
      console.log('开始初始化');
      this.removeAllChildren();
      this.y = this.stageHeight / 4 * 2;
      this.createKeys();
    },
    createKeys: function () {
      this.initStartKey();
      var notation = ns.notation;
      var current_beatNum = 0;
      for (var i = 0; i < notation.length; i++) {
        var item = notation[i];
        this.createSingleKey(item, i, current_beatNum);
        /* 小数点累加会报错，beatNum乘上1000 后面取到数据需要 除1000 */
        current_beatNum = current_beatNum + item.beatNum * 1000;
      }
      this.total_beatNum = current_beatNum;
      this.total_height = current_beatNum / 1000 * ns.keyBaseHeight * 2;
      console.log('按键创建完毕');
    },
    // 初始化开始按键
    initStartKey: function () {
      var randomNum = this.getRandomNum();
      var startX = randomNum / 4 * this.width; // 随机四个位置

      var key_startX = startX + 2;
      var key_width = this.width / 4 - 4;
      if (randomNum === 0) {
        key_startX = 0;
        key_width = this.width / 4 - 2;
      } else if (randomNum === 3) {
        key_width = this.width / 4 - 2;
      }
      var key_height = this.stageHeight / 4;
      var startImage = game.asset.start;
      this.startKey = new Hilo.Bitmap({
        image: startImage,
        id: 'startKey',
        x: key_startX,
        y: 0,
        width: key_width,
        height: key_height,
        // background: '#000000'
      }).addTo(this, 10);

      this.startKey.on(Hilo.event.POINTER_START, this.onUserclick.bind(this));
    },
    createSingleKey: function (options, index, current_beatNum) {
      var music_type = options.note / 1;
      var randomNum = this.getRandomNum();
      var startX = randomNum / 4 * this.width; // 随机四个位置
      var format_beatNum = options.beatNum;
      if (format_beatNum < 1) { // 小于1排都是短键
        format_beatNum = 1 / 2;
      } else {
        format_beatNum = options.beatNum * 2;
      }
      var key_startX = startX + 2;
      var key_width = this.width / 4 - 4;
      if (randomNum === 0) {
        key_startX = 0;
        key_width = this.width / 4 - 2;
      } else if (randomNum === 3) {
        key_width = this.width / 4 - 2;
      }
      new game.Key({
        id: 'key' + index,
        key_index: index,
        circle_img: ns.asset.circle,
        arc_img: ns.asset.arc,
        stageHeight: this.stageHeight,
        stageWidth: this.stageWidth,
        height: ns.keyBaseHeight * format_beatNum,
        width: key_width,
        startX: key_startX,
        music_type: music_type,
        beatNum: format_beatNum,
        current_beatNum: current_beatNum,
        tone: options.tone,
      }).addTo(this);
    },
    getRandomNum: function () {
      var _this = this;
      var final_randomNum = 0;
      // 生成随机数
      function createRandom() {
        var randomNum = Math.round(Math.random() * 3);
        if (randomNum === _this.last_randomNum) {
          // console.log('递归调用');
          createRandom();  // 递归调用，直到生成和上一次轨道位置不一样的随机数
        } else {
          final_randomNum = randomNum;
        }
      }

      createRandom();
      // 重置上一个的随机数
      this.last_randomNum = final_randomNum;
      return final_randomNum;
    },

    startMove: function () {
      //设置缓动时间
      this.moveTween.duration = this.total_beatNum / 1000 * ns.batTime;
      this.moveTween.delay = 100;
      //设置缓动的变换属性，即x从当前坐标变换到targetX
      this.moveTween.setProps({y: this.y}, {y: this.total_height + this.y});
      //启动缓动动画
      this.moveTween.start();
    },
    // 开始游戏
    onUserclick: function () {
      this.startKey.alpha = 0.2;
      game.onUserInput();
    },

    stopMove: function () {
      if (this.moveTween) this.moveTween.pause();
    },
    // 移动结束
    moveEnd: function () {
      console.log('移动结束');
      setTimeout(function () {
        game.gameOver();
      }, 500);
    }

  });

})(window.game);